<template>
	<view>
		<view class="u-padding-20">
			<card :list='Coupond' @buy='buy'></card>
		</view>
		
		
	
		<!-- <u-mask :show="show" >
			<view class="empty" @click="close">
				<view class="txt"> 
					恭喜你获得洗车优惠{{num}}次
				</view>
				<image style="width:500rpx ;height: 600rpx;" src="/static/getCoupond.png" mode="aspectFill"></image>
			</view>
		</u-mask> -->
	</view>
</template>

<script>
	import payway from '../../common/wxpay.js';
	import card from '../list/component/card.vue'
	export default{
		components: {
					card
		},
		data(){
			return{
				Coupond:[],
				show:false,
				num:"",
				sid:''
			}
		},
		onLoad(options) {
			
			if (options.q) {
				let url=decodeURIComponent(options.q);
				let params=this.getQueryString(url);
				this.sid=params.sid
				this.get(params.id,params.sid)
			}
		},
		methods:{
			
			color(params) {
				var colorList = ['to right, #FF6D8A,#FF4962', 'to right, #FFB916,#FE8F1E','to right, #7AC2FE,#3F96FF'];
				var colorList2 = [' #FF6D8A', '#FFB916',' #7AC2FE'];
				params.forEach((item,index)=>{
					item.color=colorList2[index % colorList2.length]
					item.bg=colorList[index % colorList.length]
				})
				return params
			},
			get(id,sid){
					this.$u.post('/api/index/card', {
						com_id: sid,
					}).then(res => {
						var arr= this.color(res);
						this.Coupond = arr.filter(item=>item.id==id);	
					}).catch(err => {
				
					})
				
			},
			buy(item) {
				this.$u.post('/api/indent/buyCard', {
					card_id: item.id,
					com_id: this.sid,
				}).then(res => {
					if (res == null) {
						this.$refs.uToast.show({
							title: '领取成功',
							type: 'success',
							callback:()=>{
								uni.switchTab({
									url:'/pages/index/index'
								})
							}
			
						})
					} else {
						var packages = res.package;
						var {
							noncestr,
							timestamp,
							signtype,
							paysign
						} = res
						payway.wx(timestamp, noncestr, packages, signtype, paysign).then(
							res => {
								this.$refs.uToast.show({
									title: '购买成功',
									type: 'success',
									callback:()=>{
										uni.switchTab({
											url:'/pages/index/index'
										})
									}
								})
							}).catch(err => {})
					}
				}).catch(err => {
			
				})
			
			
			},
			getQueryString(url) {
			  let requestParams = {};
			  if (url.indexOf("?") !== -1) {
				let str = url.substr(url.indexOf("?") + 1); //截取?后面的内容作为字符串
				let strs = str.split("&"); //将字符串内容以&分隔为一个数组
				for (let i = 0; i < strs.length; i++) {
				  requestParams[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
				  // 将数组元素中'='左边的内容作为对象的属性名，'='右边的内容作为对象对应属性的属性值
				}
			  }
			  return requestParams;
			},
			close(){
				uni.switchTab({
					url:'/pages/index/index'
				})
			}
		}
	}
</script>

<style>
	.txt{
		position: absolute;
		top: 0;
		z-index: 999;
		width: 300rpx;
		text-align: center;
		margin:220rpx auto;
		left: 100rpx;
		background: #C66100;
		color: #fff;
		padding:  15rpx 0;
		border-radius: 40rpx;
		font-size: 26rpx;
		
	}
</style>
